package applet.utils;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import app.io.LoadWebFile;
import app.io.MainPanel;

public class LoadFile extends JApplet implements ActionListener{

	private final String sload = "Load from ...";
	private final String sloadtt = "Click to load a remote file, see the stipulated syntax guide.";
	private final String cload = "load";
	private final String csel = "selected";
	private String[] urls = {"http://localhost/sample.kbox", "http://localhost/sample.qbox"};
	private JComboBox box;
	private ImageIcon kbimage;
	//private Icon icfile;
	
	public void init(){
		try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                	//icfile = (Icon) getImage(getCodeBase(), "kb.gif");
                    createPanel();
                }
            });
        } catch (Exception e) {
            System.err.println("Creation of Panel failed!");
        }

	}
	
	public void start(){
		
	}
	
	public void stop(){
		
	}
	public void destroy(){
		
	}
	
	private void createPanel(){
		MainPanel mp = new MainPanel();
		this.setSize(700, 700);
		mp.setSize(this.getSize().width, this.getSize().height);
		JTabbedPane tab = mp.getTabs();
		
		tab.setSize(mp.getSize().width, mp.getSize().height - 50);
		
		System.out.println(getCodeBase());
		this.kbimage = new ImageIcon(this.getImage(getCodeBase(), "images/kb.gif"));
		if(this.kbimage == null){
			showStatus("Failed to load Kb image.");
		}
		tab.addTab("KB", this.kbimage, mp.getScrollPane());
		box = new JComboBox(urls);
		box.setEditable(true);
		box.addActionListener(this);
		box.setActionCommand(csel);
		box.setToolTipText("Select or enter a URL, then press Enter or the button.");
		//
		JButton loadbt = new JButton(sload);
		loadbt.setHorizontalAlignment(SwingConstants.CENTER);
		loadbt.setVerticalAlignment(SwingConstants.CENTER);
		loadbt.setToolTipText(sloadtt);
		//loadbt.setSize(icfile.getIconWidth()*2, icfile.getIconHeight());
		loadbt.setActionCommand(cload);
		this.setContentPane(mp);
		mp.add(loadbt);
		mp.add(box);
		loadbt.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals(cload) || e.getActionCommand().equals(csel)){
			String urlstring = (String) box.getSelectedItem();
			showStatus("Loading " + urlstring);
			if(!new LoadWebFile(urlstring, false).resolveFile()){
//				try
//				{
//				getAppletContext().showDocument(new URL("javascript:CloseMe()"));
//				}
//				catch (MalformedURLException me){}
			}
			//load a file
//	    	JFileChooser fileChooser = 
//    	    	new JFileChooser("");
//	    	fileChooser.setDialogTitle("Select the KB file ...");
//	    	fileChooser.setApproveButtonText("Confirm");
//	    	fileChooser.setAcceptAllFileFilterUsed(false);
//	    	fileChooser.addChoosableFileFilter(new KBFileFilter(false));
//	    	fileChooser.showOpenDialog(null);
//	    	File kbFile = fileChooser.getSelectedFile();
//	        if (kbFile != null) {
//	           //do sth
//	        }
		}
	}


}
